<!--工程划分-->
<template>
  <div class="department" id="department">
    <!-- 左侧部门管理 -->
    <div class="structureTree" v-if="treelength">
      <div class="structureTree_title">
        <div>组织机构</div>
        <div>
          <!-- <el-checkbox v-model="checked"  @change="see()">查看下级</el-checkbox> -->
        </div>
      </div>

      <tree
        :treeData="treeData"
        :checkData="checkData"
        nodeKey="fCompanyid"
        @getTreeInfo="handleNodeClick"
        :defaultProps="defaultProps"
      >
      </tree>
    </div>
    <!-- 右侧 -->
    <div class="department_right">
      <!-- 部门信息 -->
      <div class="department_right_name">
        <div>{{ showName }}</div>
      </div>
      <!-- 顶部导航栏 -->
      <div class="mechanism_nav">
        <!-- 左侧 -->
        <div class="mechanism_nav_left"></div>
        <!-- 右侧 -->
        <div class="print">
          <div class="printTable">
            <el-button
              type="primary"
              icon="el-icon-refresh"
              size="small"
              @click="getDataList(getList)"
            ></el-button>
          </div>
        </div>
      </div>

      <!-- 表格 -->
      <div class="text_table">
        <el-table
          ref="multipleTable"
          :data="tableData5"
          style="width: 100%"
          height="100%"
          lazy
          :tree-props="{
            children: 'children',
            hasChildren: 'IsLeaf',
          }"
          row-key="Code"
          v-loading="loading"
          element-loading-text="拼命加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)"
          :row-class-name="tableRowClassName"
          border
          stripe
          highlight-current-row
          @current-change="rowChange"
          @row-click="getRowInfo"
        >
          <el-table-column
            fixed
            type="index"
            label="序号"
            width="50"
            align="center"
          ></el-table-column>
          <!-- <el-table-column
            prop="Code"
            label="工程编号"
            min-width="200"
            style="display: flex"
          >
            <template slot-scope="scope">
              <a href="javascript:;" :title="scope.row.Code">
                {{ scope.row.Code }}
              </a>
            </template>
          </el-table-column>
          <el-table-column
            prop="Name"
            label="工程名称"
            align="center"
            width="300"
          >
            <template slot-scope="scope">
              <a href="javascript:;" :title="scope.row.Name">
                {{ scope.row.Name }}
              </a>
            </template>
          </el-table-column> -->
          <el-table-column
            prop="CodeName"
            label="工程部位"
            min-width="200"
            style="display: flex"
          >
            <template slot-scope="scope">
              <a href="javascript:;" :title="scope.row.CodeName">
                {{ scope.row.CodeName }}
              </a>
            </template>
          </el-table-column>
          <el-table-column
            prop="FullCodeName"
            label="工程类别"
            align="left"
            width="240"
          >
            <template slot-scope="scope">
              <a href="javascript:;" :title="scope.row.FullCodeName">
                {{ scope.row.FullCodeName }}
              </a>
            </template>
          </el-table-column>
          <el-table-column
            prop="CodeType"
            label="工程划分"
            width="110"
            align="left"
          >
            <template slot-scope="scope">
              <a href="javascript:;" :title="scope.row.CodeTypeText">
                {{ scope.row.CodeTypeText }}
              </a>
            </template>
          </el-table-column>
          <el-table-column
            prop="stepCount"
            label="工序总数(条)"
            width="120"
            align="center"
          >
            <template slot-scope="scope">
              <a href="javascript:;" :title="scope.row.stepCount">
                {{ scope.row.stepCount }}
              </a>
            </template>
          </el-table-column>
          <el-table-column
            prop="completedStepCount"
            label="完成报验(条)"
            width="120"
            align="center"
          >
            <template slot-scope="scope">
              <a href="javascript:;" :title="scope.row.completedStepCount">
                {{ scope.row.completedStepCount }}
              </a>
            </template>
          </el-table-column>
          <el-table-column
            prop="completedStepRate"
            label="完成率(%)"
            width="120"
            align="center"
          >
            <template slot-scope="scope">
              <a href="javascript:;" :title="scope.row.completedStepRate">
                {{ scope.row.completedStepRate }}
              </a>
            </template>
          </el-table-column>
          <el-table-column
            prop="disposableStepCount"
            label="一次性合格(条)"
            width="120"
            align="center"
          >
            <template slot-scope="scope">
              <a href="javascript:;" :title="scope.row.disposableStepCount">
                {{ scope.row.disposableStepCount }}
              </a>
            </template>
          </el-table-column>
          <el-table-column
            prop="disposableStepRate"
            label="一次通过率(%)"
            width="110"
            align="center"
          >
            <template slot-scope="scope">
              <a href="javascript:;" :title="scope.row.disposableStepRate">
                {{ scope.row.disposableStepRate }}
              </a>
            </template>
          </el-table-column>
          <el-table-column
            prop="concealedStepCount"
            label="隐蔽工序(条)"
            width="110"
            align="center"
          >
            <template slot-scope="scope">
              <a
                href="javascript:;"
                :title="scope.row.concealedStepCount"
                style="color: #409eff"
                @click="openLink(scope.row)"
              >
                {{ scope.row.concealedStepCount }}
              </a>
            </template>
          </el-table-column>
          <template slot="empty">
            <img
              style="width: 285px; height: 208px"
              src="@/assets/img/undata.png"
              alt=""
            />
            <div class="pageFlagText">暂时没有数据哦！</div>
          </template>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getXmCompanyTreeList, //机构树
  getWbsTreeList, //列表页
  stationWorkToken, //获取token
  GetShanXiStepFiles, //获取隐蔽工序文件
} from "@/api/request.js";
import tree from "@/components/QualityGridManage/tree"; //左侧机构树

export default {
  data() {
    return {
      name: "stationWorkList",
      row: {},
      showName: "未选择公司",
      checked: false, //查看下级
      fId: null, //当前数据fId
      loading: false,
      tableData5: [],
      formData: {},
      treeData: [],
      tableData: [],
      defaultProps: {
        children: "children",
        label: "fShortname",
      },
      companyId: null,
      treeText: {
        companyId: null,
        userId: null,
      },
      treelength: false,
      checkData: [],
      getList: {
        //传标段id(目前只有青银二广点击标段有数据)(1标:a1d978df-6665-4836-b892-5368af07dedd 2标:4ec357bf-6fbb-474d-9345-e57bad661ce7 3标:f8c992c1-a9e9-4d10-a784-4f3b9f0b588d)
        orgCode: "",
        parentCode: "", //第一级传0,子级取上一级Code
      },
      WbsRow: null, //选中行
      token: "",
    };
  },
  components: {
    tree,
  },
  created() {},
  mounted() {
    this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
    if (JSON.parse(sessionStorage.getItem("Quality_treeCheck"))) {
      var id = JSON.parse(sessionStorage.getItem("Quality_treeCheck"));
      this.checkData = [id.fCompanyid];
    }

    this.getTree();
  },
  methods: {
    // 左侧树结构
    getTree() {
      this.treeText.companyId = this.userInfo.companyId;
      this.treeText.userId = this.userInfo.userId;
      getXmCompanyTreeList(this.treeText)
        .then((res) => {
          if (res.errcode == 0) {
            var companyTree = res.data.userCompanyTreeList;
            this.treeData = this.commonJs.tranListToTreeData(companyTree);

            if (this.treeData[0].children == undefined) {
              this.treelength = false;
            } else {
              this.treelength = true;
            }

            if (this.checkData[0]) {
              this.showName = JSON.parse(
                sessionStorage.getItem("Quality_treeCheck")
              ).fShortname;

              this.companyId = this.checkData[0];
            } else {
              this.showName = this.treeData[0].fShortname; //当前点击组织机构名称
              this.companyId = this.treeData[0].fCompanyid; //当前点击组织机构ID
              this.checkData[0] = this.treeData[0].fCompanyid;
            }
            this.getList = {
              //传标段id(目前只有青银二广点击标段有数据)(1标:a1d978df-6665-4836-b892-5368af07dedd 2标:4ec357bf-6fbb-474d-9345-e57bad661ce7 3标:f8c992c1-a9e9-4d10-a784-4f3b9f0b588d)
              orgCode: this.companyId,
              parentCode: "0", //第一级传0,子级取上一级Code
            };
            // 列表
            this.getDataList(this.getList);
            // 列表
          } else {
            this.$message.error(res.msg);
            // this.$message.error("获取组织机构失败");
            return false;
          }
        })
        .catch(() => {
          return false;
        });
    },

    // 获取列表数据
    getDataList(id) {
      console.log(id);
      // var bbb = {
      //   orgCode: "a1d978df-6665-4836-b892-5368af07dedd",
      //   parentCode: "0", //第一级传0,子级取上一级Code
      // };
      getWbsTreeList(id)
        .then((res) => {
          if (res.errcode == 0) {
            var that = this;
            setTimeout(() => {
              that.loading = false;
              that.tableData5 = res.data.data;
              that.tableData5.map((item) => {
                item.CodeName = item.Code + "、" + item.Name;
                if (item.CodeType == 1) {
                  item.CodeTypeText = "单位工程";
                } else if (item.CodeType == 2) {
                  item.CodeTypeText = "子单位工程";
                } else if (item.CodeType == 3) {
                  item.CodeTypeText = "分部工程";
                } else if (item.CodeType == 4) {
                  item.CodeTypeText = "子分部工程";
                } else if (item.CodeType == 5) {
                  item.CodeTypeText = "分项工程";
                } else if (item.CodeType == 6) {
                  item.CodeTypeText = "子分项工程";
                } else {
                  item.CodeTypeText = "/";
                }
                // 工序总数
                if (item.stepCount == 0) {
                  item.stepCount = "";
                }
                // 完成报验
                if (item.completedStepCount == 0) {
                  item.completedStepCount = "";
                }
                // 完成率
                if (item.completedStepRate == 0) {
                  item.completedStepRate = "";
                }
                // 一次性合格
                if (item.disposableStepCount == 0) {
                  item.disposableStepCount = "";
                }
                // 一次通过率
                if (item.disposableStepRate == 0) {
                  item.disposableStepRate = "";
                }
                // 隐蔽工序
                if (item.concealedStepCount == 0) {
                  item.concealedStepCount = "";
                }
              });
            }, 200);
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    // 点击当前行
    getRowInfo(e) {
      this.fId = e.F_EnCode;
      this.row = e;
    },
    // 行颜色
    tableRowClassName({ row }) {
      if (
        (row.ParentCode == 0 && !row.children) ||
        (row.ParentCode == null && !row.children)
      ) {
        return "fristColor";
      }
    },
    //点击当前行变色
    rowChange(val) {
      this.WbsRow = val;
    },
    //点击树
    handleNodeClick(data) {
      console.log("点击", data);
      this.showName = data.fShortname; //顶部显示机构名
      this.getList = {
        //传标段id(目前只有青银二广点击标段有数据)(1标:a1d978df-6665-4836-b892-5368af07dedd 2标:4ec357bf-6fbb-474d-9345-e57bad661ce7 3标:f8c992c1-a9e9-4d10-a784-4f3b9f0b588d)
        orgCode: data.fCompanyid,
        parentCode: "0", //第一级传0,子级取上一级Code
      };
      this.getDataList(this.getList);
    },

    // 点击隐蔽条数
    openLink(row) {
      console.log("点击隐蔽条数", row);
      this.getToken(row);
    },
    // 获取token
    getToken(row) {
      var data = {
        username: "sxlq",
        password: "sxlq.123",
        client_secret: "0462e562-0c92-4b69-b577-5bde7e6d1fc3",
        client_id: "2338924428965001937",
        grant_type: "password",
        scope: "private",
        connection_id: "qyegllx",
      };
      stationWorkToken(data)
        .then((res) => {
          this.token = res.token_type + " " + res.access_token;

          var text = {
            ParentCode: row.ParentCode,
            Code: row.Code,
          };

          this.getConcealedStepCount(text, this.token);
        })
        .catch(() => {
          console.log("获取token失败");
        });
    },
    //获取隐蔽工序文件
    getConcealedStepCount(data, token) {
      GetShanXiStepFiles(data, token)
        .then((res) => {
          if (res.success) {
            console.log("获取隐蔽工序文件", res);
            window.open(
              "http://39.106.10.1:2711/Upload" + res.data.Files[0].FilePath
            );
          } else {
            this.$message.error("获取隐蔽工序文件获取失败");
          }
        })
        .catch(() => {
          console.log("获取隐蔽工序文件接口报错");
        });
    },
  },
};
</script>

<style scoped>
</style>